﻿using UnityEngine;
using System.Collections;
using Slate;
using XSlate.Runtime;
#if UNITY_EDITOR
using Sirenix.OdinInspector;
using Sirenix.Serialization;
#endif
using System.IO;

namespace XSlate {
    public class XCutScene : Cutscene
    {
        [LabelText("CutScene名称")]
        [SerializeField]
        private string CutSceneName = "";

        [Button("导出技能数据", 35), GUIColor(0.78f, 0.23f, 0.56f)]
        public void ExportSkillDatas()
        {
            if (string.IsNullOrEmpty(CutSceneName)) {
                return;
            }
            XCutSceneData xCutSceneData = new XCutSceneData();
            foreach (var gCutSceneGroup in this.groups)
            {
                if (gCutSceneGroup is XGroup xGroup) {
                    XGroupData xGroupData = new XGroupData();
                    foreach (var cutSceneTrack in xGroup.tracks) {
                        XTrackData xTrackData = new XTrackData();
                        foreach (var xTrackClip in cutSceneTrack.clips) {
                            XStateActionBase action = xTrackClip as XStateActionBase;
                            XDirectableData actionData = action.BindingDate;
                            actionData.RelativelyEndTime = (long)(action.endTime * 1000);
                            actionData.RelativelyStartTime = (long)(action.startTime * 1000);
                            xTrackData.ActionDatas.Add(actionData as XActionData);
                        }
                        xGroupData.TrackDatas.Add(xTrackData);
                    }
                    xCutSceneData.GroupDatas.Add(xGroupData);
                }
            }

            using (FileStream file = File.Create($"Assets/{CutSceneName}.byte")) {
                byte[] serResult = SerializationUtility.SerializeValue(xCutSceneData,DataFormat.Binary);
                file.Write(serResult,0,serResult.Length);
            }

        }
    }

}
